﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace ebc.patterns.rx.tests
{
    [TestFixture]
    public class test_Observable_Join
    {
        [Test]
        public void Join_observables_with_just_one_item_each()
        {
            var left = new Subject<int>();
            var right = new Subject<string>();

            var result = new Tuple<int, string>(0, "");
            var joined = left.Join(right);
            joined.Subscribe(_ => result = _);

            right.OnNext("hello");
            left.OnNext(42);

            Assert.AreEqual(42, result.Item1);
            Assert.AreEqual("hello", result.Item2);
        }


        [Test]
        public void Join_observables_with_multiple_items()
        {
            var left = new Subject<int>();
            var right = new Subject<string>();

            var results = new List<Tuple<int, string>>();
            var joined = left.Join(right);
            joined.Subscribe(results.Add);

            right.OnNext("hello");
            left.OnNext(42);

            left.OnNext(99);
            Assert.AreEqual(1, results.Count);
            right.OnNext("world");
            Assert.AreEqual(2, results.Count);

            Assert.AreEqual(99, results[1].Item1);
            Assert.AreEqual("world", results[1].Item2);
        }


        [Test]
        public void Join_on_unbalanced_influx()
        {
            var left = new Subject<int>();
            var right = new Subject<string>();

            var result = new Tuple<int, string>(0, "");
            var joined = left.Join(right);
            joined.Subscribe(_ => result = _);

            right.OnNext("hello");
            right.OnNext("world");
            left.OnNext(42);

            Assert.AreEqual(42, result.Item1);
            Assert.AreEqual("hello", result.Item2);
        }


        [Test]
        public void Check_zip()
        {
            var left = new Subject<int>();
            var right = new Subject<string>();

            var joined = left.Zip(right, (l,r) => new Tuple<int, string>(l,r));
            joined.Subscribe(Console.WriteLine);

            right.OnNext("hello");
            right.OnNext("world");
            left.OnNext(42);
            right.OnNext("how");
            left.OnNext(99);
            right.OnNext("wow");
            left.OnNext(17);
        }
    }
}
